package it.xiuxian.tradecenter.fragment



import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.bytedance.sdk.dp.DPWidgetDrawParams
import com.bytedance.sdk.dp.IDPDrawListener
import com.bytedance.sdk.dp.IDPWidget
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.utils.MediaServiceImpl
import it.xiuxian.tradecenter.R
import it.xiuxian.tradecenter.databinding.FragmentTradcenterBinding


@Route(path = ArouterAddress.TRADCENTERFRAGMENT)
class TradCenterFragment: Fragment(){
    private  val TAG = "TradCenterFragment"
  lateinit var binding:FragmentTradcenterBinding

    private var dpWidget: IDPWidget? = null
    private var userVisible = false
    companion object{
        fun newInstance():Fragment{
            return TradCenterFragment()
        }
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = LayoutInflater.from(this.context).inflate(R.layout.fragment_tradcenter,null,false)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initDrawWidget()
    }
    override fun onResume(){
        super.onResume()
        Log.d(TAG, "onResume")
        dpWidget?.fragment?.onResume()
    }

    override fun onPause(){
        super.onPause()
        Log.d(TAG, "onPause")
        dpWidget?.fragment?.onPause()
    }

    override fun onHiddenChanged(hidden: Boolean){
        super.onHiddenChanged(hidden)
        Log.d(TAG, "onHiddenChanged ${hidden}")
        dpWidget?.fragment?.onHiddenChanged(hidden)
    }

    override fun onDestroyView(){
        super.onDestroyView()
        dpWidget?.fragment?.onDestroyView()
    }

    override fun onDestroy(){
        super.onDestroy()
      dpWidget?.fragment?.onDestroy()
    }
    private fun initDrawWidget(){
        if (!MediaServiceImpl.Companion.instance.isDPInited()){
            this.view?.postDelayed({
                initDrawWidget()
            }, 100)
            return
        }
        dpWidget = DPHolder.getInstance().buildDrawWidget(
            DPWidgetDrawParams.obtain()/*.liveAdCodeId("946413654").liveNativeAdCodeId("946413669")*/
                .adOffset(0) //单位 dp，为 0 时可以不设置
                .drawContentType(DPWidgetDrawParams.DRAW_CONTENT_TYPE_ALL)
                .hideClose(false, null).listener(object : IDPDrawListener() {
                    override fun onDPRefreshFinish(){
                        Log.d(TAG, "onDPRefreshFinish")
                    }

                    override fun onDPPageChange(position: Int) {
                        Log.d(TAG, "onDPPageChange: $position")
                    }

                    override fun onDPVideoPlay(map: Map<String, Any>) {
                        Log.d(TAG, "onDPVideoPlay")
                    }

                    override fun onDPVideoCompletion(map: Map<String, Any>){
                        Log.d(TAG, "onDPVideoCompletion: ")
                    }

                    override fun onDPVideoOver(map: Map<String, Any>){
                        Log.d(TAG, "onDPVideoOver")
                    }

                    override fun onDPClose(){
                        Log.d(TAG, "onDPClose")
                    }

                    override fun onDPRequestStart(map: Map<String, Any>?){
                        Log.d(TAG, "onDPRequestStart")
                    }

                    override fun onDPRequestSuccess(list: List<Map<String, Any>>){
                        Log.d(TAG, "onDPRequestSuccess")
                    }

                    override fun onDPRequestFail(
                        code: Int, msg: String, map: Map<String, Any>?
                    ) {
                        Log.d(TAG, "onDPRequestFail")
                    }

                    override fun onDPClickAuthorName(map: Map<String, Any>) {
                        Log.d(TAG, "onDPClickAuthorName")
                    }

                    override fun onDPClickAvatar(map: Map<String, Any>) {
                        Log.d(TAG, "onDPClickAvatar")
                    }

                    override fun onDPClickComment(map: Map<String, Any>) {
                        Log.d(TAG, "onDPClickComment")
                    }

                    override fun onDPClickLike(
                        isLike: Boolean, map: Map<String, Any>
                    ) {
                        Log.d(TAG, "onDPClickLike")
                    }

                    override fun onDPVideoPause(map: Map<String, Any>) {
                        Log.d(TAG, "onDPVideoPause")
                    }

                    override fun onDPVideoContinue(map: Map<String, Any>) {
                        Log.d(TAG, "onDPVideoContinue")
                    }

                    override fun onDPClickShare(map: MutableMap<String, Any>?) {
                        Log.d(TAG, "onDPClickShare $map")
                    }
                })
        )
        dpWidget?.let {
            childFragmentManager.beginTransaction().apply {
                replace(R.id.fl_container, it.fragment)
                this.commitNow()
            }
        }
    }

}


